$(function() {
	var totalPage;
	var object = {};
	var information = {curFLow:1};
	var page = 1;
	var deleteid;
	requestClassification(); //渠道
	requestSourceSel(); //分级 
	requestSort();
	requestRefer(); //已提交数据

	//删除按钮点击后

	
		//查询
	$("#firstSearch").click(function() {
			firstSearch();
	})
	
		//分页，首页
	$("#headPage").click(function() {
		page = 1;
		$("#totalPage").html(page + "/" + totalPage);
		requestPage(page);
	})

	$(document).on("click", ".info-sub-a", function() {
		var id = $(this).attr("data-id");
		var url="SubToFirstCheck.do";
		myTool.alert({		
			title:"提示",
			context:"是否确认提交信息?",
			isMask:true,
			callback:function(){
				YuXi.util.get(url, {infoId: id}, function(data) {
					if(data.status){
						myTool.alert({		
							title:"提示",
							context:"提交信息成功。",
							isMask:true,
							callback:function(){
								window.location.reload();
							
							}
						});
					}
				},function(){
					
				});
				
			}
		});
		
	});
	//分页，尾页
	$("#endPage").click(function() {
			$("#totalPage").html(totalPage + "/" + totalPage);
			page = totalPage;
			requestPage(page);
		})
		//分页，上一页
	$("#prevPage").click(function() {
			if (page <= 1) {
				page = 1;
			} else {
				page = page - 1;
				$("#totalPage").html(page + "/" + totalPage);
				requestPage(page);
			}

		})
		//分页，下一页
	$("#nextPage").click(function() {

		if (page >= totalPage) {
			page = page - 1;
		} else {
			page = page + 1;
			$("#totalPage").html(page + "/" + totalPage);
			requestPage(page);
		}

	})
	
	function requestSort(){	
	var url="getAllInfomationSort.do";//分类地址
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		object=data.object;
		//用data循环添加分类信息
		for(var i=0;i<object.length;i++)
		{
			var html="<option  value="+object[i].id+" >"+object[i].name+"</option>";
			$(".reSearchsort").append(html);
		}
	},function(){
		
	});//请求数据
	
	}
	
	function requestClassification() {
		var url = "getAllInformationClassification.do"; //分级地址
		var data = {};
		var object = {};
		data = YuXi.util.get(url, "null", function(data) {
			object = data.object;
			//用data循环添加分口信息
			for (var i = 0; i < object.length; i++) {
				var html = "<option  value=" + object[i].id + " >" + object[i].name + "</option>";
				$(".reSearchpes").append(html);
			}
		}, function() {

		}); //请求数据
	}

	function requestSourceSel() {
		var url = "getAllInfomationSource.do"; //渠道地址
		var data = {};
		var object = {};
		data = YuXi.util.get(url, "null", function(data) {
			//console.log(data);
			object = data.object;
			for (var i = 0; i < object.length; i++) {
				var html = "<option value=" + object[i].id + ">" + object[i].name + "</option>";
				$(".reSearchSource ").append(html);
			}

		}, function() {

		});
	}

	//跳转到详情界面
	$("#tablebody").parent().on("click", ".detail", function() {
			var id = $(this).attr("data-id");
			var menuId=$("#menu-container").attr("menu-id");		
			window.location.href = 'subDetailInfo.jsp?menuId='+menuId+'&detailId=' + id;
			/* location.search = "?detailId="+id;*/
		})
		//跳转到修改界面
	$("#tablebody").parent().on("click", ".update", function() {
		var id = $(this).attr("data-id");
		var flowId = $(this).attr("data-flowid");
		window.location.href = 'updateInfo.jsp?updateId=' + id + '&flowId=' + flowId;
	})
	
	$(document).on("click", ".delete", function() {	
			deleteid = $(this).attr("id");
			myTool.alert({
				title:"删除确认",
				context:"你确定要删除此条信息吗?",
				callback:function(){
					requestDele(deleteid);
				},
				isMask:true
			});
		})

		//删除确认
	function requestDele(id) {
		var dataid = {
			id: id
		};
		var url = "userDelInformationById.do" //删除信息地址;
		YuXi.util.get(url, dataid, function(data) {
			$("#" + id).parents("tr").remove();
			$("#successmodel").modal('show');

		}, function() {
			$("#badmodal").modal('show');
		})
	}
	//分页
	function requestPage(page) {
		$("#tablebody").html("");
		information.currentPage = page;
		var url = "getUserInformation.do";
		YuXi.util.get(url, information, function(data) {
			object = data.list;
			addInformation(object);
		}, function() {
			//console.log("分页出错");
		})
	}
	
	//向后台请求已提交的信息数据
	function requestRefer() {
		var url = "getUserInformation.do"; //已提交
		YuXi.util.get(url, {curFLow:1}, function(data) {
			totalPage = data.totalPage;
			$("#totalPage").html("1/" + totalPage);
			object = data.list;
			addInformation(object);
		}, function() {

		});

	}

	//简介，分级，渠道，状态，时间（今天,一周...）查找
	function firstSearch() {

		information.key = $("#profile").val(); //简介
		var curFlowText = $(".state option:selected").val();
		curFlowText == null ? "" : information.curFLow = curFlowText; //当前状态
		information.informationSource = $(".reSearchSource option:selected").val(); //来源渠道
		information.informationLv = $(".reSearchpes option:selected").val() ;//分级
		information.informationSort=$(".reSearchsort option:selected").val();//分类
		
		var array = [];
		var starTime=$("#starTime").val();
		var endTime=$("#endTime").val();
		array=YuXi.timestamp.get(starTime,endTime,null);
		information.startTimestamp = array[0];
		information.endTimestamp = array[1];
		information.currentPage=1;
		//查询数据传送给后台
		var url = "getUserInformation.do"; //地址----------------------------------
		YuXi.util.get(url, information, function(data) {
			totalPage = data.totalPage;
			$("#totalPage").html("1/" + totalPage);
			page = 1;
			var object = data.list;
			$("#tablebody").html("");
			addInformation(object);
		}, function() {
			//console.log("查询失败");
		});

	}

	function addInformation(object) {
		for (var i = 0; i < object.length; i++) {
			var time = YuXi.getTime.get(object[i].createTime);
			if (object[i].informationProfile.length > 15)
				object[i].informationProfile = object[i].informationProfile.substring(0, 15) + "...";
			if (object[i].flowName == "信息管理") {
				var html = "<tr ><td>" + object[i].informationAccount + 
				"</td><td >" + object[i].informationProfile + "</td>" +
				"</td><td >" + object[i].informationSortNames + "</td>" +
				"</td><td >" + object[i].informationClassificationNames + "</td>" +
				"</td><td >" + object[i].informationSourceNames + "</td>" +
				"<td >" + time + "</td><td>" + object[i].flowName + "</td>" +
				"<td><a data-id=" + object[i].id + 
				" href='javascript:void(0)'; class='detail'>详情</a> <a data-flowId=" + object[i].flowId + " data-id=" + object[i].id + " href='javascript:void(0)'; class='update'>修改</a> <a id=" + object[i].id + " href='javascript:void(0);' class='delete'>删除</a>  <a data-id=" + object[i].id + " href='javascript:void(0)'; class='info-sub-a'>提交</a></td></tr>";
			} else {
				var html = "<tr ><td>" + object[i].informationAccount + "</td><td >" + object[i].informationProfile + "</td>" +
				"</td><td >" + object[i].informationSortNames + "</td>" +
				"</td><td >" + object[i].informationClassificationNames + "</td>" +
				"</td><td >" + object[i].informationSourceNames + "</td>" +
					"<td >" + time + "</td><td>" + object[i].flowName + "</td>" +
					"<td><a data-id=" + object[i].id + " href='javascript:void(0)'; class='detail'>详情</a></td></tr>";

			}
			$("#tablebody").append(html);


		}
	}

})